(function() {
    _$(document).ready(function () {
        function App() {
        };
        function excludeSpecial(s) {
            // 去掉转义字符
            s = s.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
            // 去掉特殊字符
            s = s.replace(/[\@\#\$\%\^\&\*\{\}\:\"\L\<\>\?]/);
            return s;
        };
        var page=0;
        var tag=1;
        var kw=excludeSpecial(getQueryString('kw'));
        var jzzHTML='<div class="demo-block" id="jzz"><div class="ui-loading-wrap"><i class="ui-loading"></i> <p>加载中...</p></div></div>';
        App.prototype.ajaxFn = function () {
            _$.ajax({
                type: 'post',
                url: '/setHotsearch',
                data: {kw: kw},
                async: false,
                success: function (data) {
                }
            });
            var tath=this;
            _$.ajax({
                type: 'post',
                url: '/getSearchBook',
                data: {kw: kw, tag: 1, page: 0},
                async: false,
                success: function (json) {
                    var dropload = $('.khfxWarp').dropload({
                        scrollArea: window,
                        domDown: {
                            domClass: 'dropload-down',
                            domRefresh: '<div class="dropload-refresh">↑上拉加载</div>',
                            domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                            domNoData: '<div class="dropload-noData">已无数据</div>'
                        },
                        //上拉
                        loadDownFn: function (me) {
                            var pageLength = json['length'];
                            if (json['length'] % 10 > 0) {
                                pageLength = (json['length'] / 10) + 1;
                            }
                            if (page > pageLength) {
                                me.lock();
                                me.noData();
                                me.resetload();
                            } else {
                                console.log(page);
                                _$.ajax({
                                    type: 'post',
                                    url: '/getSearchBook',
                                    data: {kw: kw, tag: tag, page: page},
                                    async: false,
                                    success: function (data) {
                                        console.log(data);
                                        if (data.code == 1) {
                                            var info = data['info'];
                                            if (info == "") {
                                                me.lock();
                                                me.noData();
                                                me.resetload();
                                            } else {
                                                if (info.length > 0) {
                                                    var html = "";
                                                    for (var val in info) {
                                                        info[val]['index'] = parseInt(val) + 1;
                                                        if (info[val]['stateType'] == "T") {
                                                            info[val]['stateType'] = "连载中";
                                                        } else if (info[val]['stateType'] == "F") {
                                                            info[val]['stateType'] = "完结";
                                                        }
                                                        if (info[val]['cnt'] > 10000) {
                                                            info[val]['cnt'] = (info[val]['cnt'] / 10000).toFixed(2) + "万字";
                                                        } else {
                                                            info[val]['cnt'] = info[val]['cnt'] + "字";
                                                        }
                                                        html += template('tpl', info[val]);
                                                    }
                                                    // 为了测试，延迟1秒加载
                                                    setTimeout(function () {
                                                        // 插入加载使用 html() 重置 DOM
                                                        $("#searchUl").append(html);
                                                        page++;
                                                        // 每次数据加载完，必须重置
                                                        me.resetload();
                                                    }, 1000);
                                                }
                                            }
                                        }else {
                                            me.lock();
                                            me.noData();
                                            me.resetload();
                                        }
                                    }
                                });
                            }
                        }
                    })
                }
            });
        };
        App.prototype.eventFn=function(){
            var tath=this;
            var searchHistory=[];
            if(localStorage.getItem('SEARCH_HISTORY')){
                searchHistory=JSON.parse(localStorage.getItem('SEARCH_HISTORY'));
            }
            searchHistory.unshift({"hisBooks":getQueryString('kw')});
            localStorage.setItem('SEARCH_HISTORY',JSON.stringify(uniqeByKeys(searchHistory,['hisBooks'])));

            _$(".module-filter-header .btn-tag").click(function(){
                _$(this).addClass('active').siblings('.btn-tag').removeClass('active');
                var value=_$(this).attr('data-value');
                $(window).scrollTop(0);
                tag=parseInt(value);
                page=0;
                _$("#searchUl").html("");
                _$.ajax({
                    type: 'post',
                    url: '/getSearchBook',
                    data: {kw: kw, tag: parseInt(value), page: 0},
                    async: false,
                    success: function (json) {
                        var dropload = $('.khfxWarp').dropload({
                            scrollArea: window,
                            domDown: {
                                domClass: 'dropload-down',
                                domRefresh: '<div class="dropload-refresh">↑上拉加载</div>',
                                domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                                domNoData: '<div class="dropload-noData">已无数据</div>'
                            },
                            //上拉
                            loadDownFn: function (me) {
                                var pageLength = json['length'];
                                if (json['length'] % 10 > 0) {
                                    pageLength = (json['length'] / 10) + 1;
                                }
                                if (page > pageLength) {
                                    me.lock();
                                    me.noData();
                                    me.resetload();
                                } else {
                                    console.log(page);
                                    _$.ajax({
                                        type: 'post',
                                        url: '/getSearchBook',
                                        data: {kw: kw, tag: tag, page: page},
                                        async: false,
                                        success: function (data) {
                                            console.log(data);
                                            if (data.code == 1) {
                                                var info = data['info'];
                                                if (info == "") {
                                                    me.lock();
                                                    me.noData();
                                                    me.resetload();
                                                } else {
                                                    if (info.length > 0) {
                                                        var html = "";
                                                        for (var val in info) {
                                                            info[val]['index'] = parseInt(val) + 1;
                                                            if (info[val]['stateType'] == "T") {
                                                                info[val]['stateType'] = "连载中";
                                                            } else if (info[val]['stateType'] == "F") {
                                                                info[val]['stateType'] = "完结";
                                                            }
                                                            if (info[val]['cnt'] > 10000) {
                                                                info[val]['cnt'] = (info[val]['cnt'] / 10000).toFixed(2) + "万字";
                                                            } else {
                                                                info[val]['cnt'] = info[val]['cnt'] + "字";
                                                            }
                                                            html += template('tpl', info[val]);
                                                        }
                                                        // 为了测试，延迟1秒加载
                                                        setTimeout(function () {
                                                            // 插入加载使用 html() 重置 DOM
                                                            $("#searchUl").append(html);
                                                            page++;
                                                            // 每次数据加载完，必须重置
                                                            me.resetload();
                                                        }, 1000);
                                                    }
                                                }
                                            }else {
                                                me.lock();
                                                me.noData();
                                                me.resetload();
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                });
            });
        };
        App.prototype.init=function(){
            //事件调用
            var tath=this;
            this.ajaxFn();
            this.eventFn();
        };
        //程序初始化
        new App().init();
    });
})();